package com.oom;

import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: tgy
 * @Date: 2020-08-05 10:23
 *
 * -XX:MaxDirectMemorySize=10M
 *
 * 在进行NIO时，为了效率，在堆外内存DirectByteBuffer实现了零拷贝，堆外内存对young gc免疫，只有在full gc的时候才被收回。
 *
 * Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
 * 	at java.nio.Bits.reserveMemory(Bits.java:694)
 * 	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
 * 	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
 * 	at com.oom.Direct_Buffer_Memory.main(Direct_Buffer_Memory.java:22)
 */
public class Direct_Buffer_Memory {

    public static void main(String[] args) {

        List<ByteBuffer> byteBuffers = new ArrayList<>();

        while (true){

            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
            byteBuffers.add(byteBuffer);
        }

    }
}
